package cn.wolfcode.crud.service.impl;

import cn.wolfcode.crud.domain.CustomerTransFer;
import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.mapper.CustomerMapper;
import cn.wolfcode.crud.mapper.CustomerTransFerMapper;
import cn.wolfcode.crud.qo.QuerObjcet;
import cn.wolfcode.crud.service.ICustomerTransFerService;
import cn.wolfcode.crud.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransFerServiceImpl implements ICustomerTransFerService {

    @Autowired
    private CustomerTransFerMapper customerTransFerMapper;
    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public void save(CustomerTransFer customerTransFer) {
        //添加时添加时间和人,修改员工id
        Employee employee=(Employee) SecurityUtils.getSubject().getPrincipal();
        //设置操作人
        customerTransFer.setOperator(employee);
        //设置操作时间
        customerTransFer.setOperateTime(new Date());
        //把客户的销售人员字段改为新的销售人员
        Long customerId = customerTransFer.getCustomer().getId();
        Long sellerId = customerTransFer.getNewSeller().getId();
        customerMapper.updateSeller(customerId,sellerId);
        customerTransFerMapper.insert(customerTransFer);
    }

    @Override
    public void delete(Long id) {
        customerTransFerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(CustomerTransFer customerTransFer) {
        customerTransFerMapper.updateByPrimaryKey(customerTransFer);
    }

    @Override
    public CustomerTransFer get(Long id) {
        return customerTransFerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransFer> listAll() {
        return customerTransFerMapper.selectAll();
    }

    @Override
    public PageInfo<CustomerTransFer> query(QuerObjcet qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<CustomerTransFer> customerTransFers = customerTransFerMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<CustomerTransFer>(customerTransFers);
    }
}
